<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/12/16
 * Time: 11:09
 */
Class LoginAction extends BaseAction{

    public function index()
    {
        $uid = $_SESSION['uid'];
        $res = M('shop')->where(array('id'=>$uid))->getField('mode');
        $this->assign('res',$res);
        if(IS_GET){
            $type = $_GET['ordtype'];
            if($type == "payed"){
                M('shop')->where(array('id'=>$uid))->setField('mode',1);

                $this->display();
            }else{
                $this->display();
            }
        }else{
            $this->display();
        }
    }

    /**
     * 注册
     */
    public function reg()
    {
        $this->display();
    }

    /**
     * 执行注册操作
     */
    public function doregist()
    {
        if(IS_POST){
            C('TOKEN_ON',false);
            $hid = isset($_POST['doact']) ? strval($_POST['doact']) : '';
            if($hid == 'doreg')
            {
                $user = D('Shop');
                $_POST['mode']=0;//注册用户
                $_POST['authmode']='#0#';//注册用户
                $_POST['authaction']='1';//认证后调整模式
                $account = $_POST['account'];
                $res = $user->where(array('account'=>$account))->find();
                if($res){
                    $data['error']=1;
                    $data['msg']="用户名已存在";
                    return $this->ajaxReturn($data);
                }
                if ($user->create()){
                    $aid=$user->add();
                    if($aid)
                    {
                        session('uid',$aid);
                        session('user',$_POST['account']);
                        session('shop_name',$_POST['shopname']);

                        $data['error']=0;
                        $data['msg']="OK";
                        $data['url']=U('Index/login/index');

                        return $this->ajaxReturn($data);
                    }else{
                        $data['error']=1;
                        $data['msg']="服务器忙，请稍候再试";
                        return $this->ajaxReturn($data);
                    }
                }else{
                    $data['error']=1;
                    $data['msg']=$user->getError();
                    return $this->ajaxReturn($data);
                }
            }else{
                $data['error']=1;
                $data['msg']="服务器忙，请稍候再试";
                return $this->ajaxReturn($data);
            }
        }else{
            $data['error']=1;
            $data['msg']="服务器忙，请稍候再试";
            return $this->ajaxReturn($data);
        }

    }

    /**
     * 登录展示
     */
    public function login()
    {
        $this->display();
    }

    /**
     * 执行登录操作
     */
    public function dologin()
    {
        if(IS_POST){
            $user = isset($_POST['user']) ? strval($_POST['user']) : '';
            $pass = isset($_POST['password']) ? strval($_POST['password']) : '';

            $userM = M('Shop');
            $uid = $userM->where(array('account'=>$user,'password'=>md5($pass)))->field('id,account,shopname,pid')->find();
            //log::write($userM->getLastSql());
            if($uid)
            {
                session('uid',$uid['id']);
                session('user',$uid['account']);
                session('shop_name',$uid['shopname']);
                session('pid',$uid['pid']);
                $data['error']=0;
                $data['msg']="";
                $data['url']=U('Index/login/index');
                return $this->ajaxReturn($data);
            }else{
                $data['error']=1;
                $data['msg']="帐号信息不正确";
                return $this->ajaxReturn($data);
            }
        }else{
            $data['error']=1;
            $data['msg']="服务器忙，请稍候再试";
            return $this->ajaxReturn($data);
        }
    }

    /**
     * 登录跳转
     */
    private function isLogin()
    {
        if(!session('uid'))
        {
            $this->redirect('index/index/log');
        }
    }

    /**
     *修改密码
     */
    public function account()
    {
        $this->isLogin();
        $this->assign('a','account');
        $this->display();
    }

    /**
     * 执行修改密码
     */
    public function doaccount()
    {
        $this->isLogin();
        $uid = session('uid');
        $pass = isset($_POST['password']) ? $_POST['password'] : '';
        if($pass)
        {
            $user = D('Shop');
            if($user->create()){
                if($user->where("id = {$uid}")->save())
                {
                    $this->success('修改密码成功');
                }else{
                    $this->error('修改密码错误');
                }
            }
        }else{
            $this->error('密码不允许为空');
        }
    }

    /**
     * 退出
     */
    public function logout()
    {
        session(null);
        $this->redirect('index/index/log');
    }
}